package com.sansui.blog.controller.admin.page;

import com.sansui.blog.pojo.Blog;
import com.sansui.blog.pojo.User;
import com.sansui.blog.pojo.vo.BlogQuery;
import com.sansui.blog.service.BlogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @name: BlogController
 * @author: Sansui
 * @Software: IDEA
 * @Blog: https://www.cnblogs.com/sansui6/
 * @date: 2022-04-04 14:53
 **/
@Controller
@RequestMapping("/admin")
public class BlogController {

    @Autowired
    private BlogService blogService;

    @GetMapping("page/blogs")
    public String adminBlogs(){
        return "admin/page/blogs";
    }

    //  获取博客接口数据
    @PostMapping("page/getBlogList")
    @ResponseBody
    public Map<String, Object> getBlogList(Integer page, Integer limit){
        Map<String, Object> map = blogService.getAllBlogByPage(page, limit);
        return map;
    }

    //  更新博客状态
    @PostMapping("page/blog/updateStatus")
    @ResponseBody
    public Map<String, Object> updateStatus(Long blogId , String status){
        Map<String, Object> map = blogService.updateStatus(blogId, status);
        return map;
    }

    //  打开添加文章页面
    @GetMapping("page/blog/add")
    public String addBlogHtml(){
        return "admin/page/blog/add";
    }

    //  打开编辑文章页面
    @GetMapping("page/blog/edit")
    public String editBlogHtml(){
        return "admin/page/blog/edit";
    }

    //新增博客信息
    @PostMapping("page/blog/create")
    @ResponseBody
    public Map<String, Object> addBlog(@RequestBody Blog blog, HttpSession session){
        Map<String,Object> data = (Map<String, Object>) session.getAttribute("user");
        Map<String, Object> map = blogService.addBlog(blog,String.valueOf(data.get("username")));
        return map;
    }

    //修改博客信息
    @PostMapping("page/blog/update")
    @ResponseBody
    public Map<String, Object> updateBlog(@RequestBody Blog blog, HttpSession session){
        Map<String,Object> data = (Map<String, Object>) session.getAttribute("user");
        Map<String, Object> map = blogService.updateBlog(blog,String.valueOf(data.get("username")));
        return map;
    }


    // 删除博客
    @PostMapping("page/blog/deleteBlog")
    @ResponseBody
    public Map<String, Object> deleteBlog(Long blogId){
//        System.out.println(blogId);
        Map<String, Object> map = blogService.deleteBlog(blogId);
        return map;
    }

    // 批量删除博客
    @PostMapping("page/blog/deleteBlogByIds")
    @ResponseBody
    public Map<String, Object> deleteBlogByIds(String blogIds){
        List<String> delList = new ArrayList<>();
        String[] bIds = blogIds.split(",");
        for (String str : bIds) {
            delList.add(str);
        };
        Map<String, Object> map = blogService.deleteBlogByIds(delList);
        return map;
    }


    //查询博客信息
    @PostMapping("page/blog/blogQuery")
    @ResponseBody
    public Map<String, Object> blogQuery(@RequestBody BlogQuery blog){
        System.out.println(blog);
        Map<String, Object> map = blogService.blogQuery(blog);
        return map;
    }




}
